﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Drawing.Design;

namespace Jemgine.Particles
{
    public class RangeConvertor : ExpandableObjectConverter
    {
        public override bool CanConvertTo(ITypeDescriptorContext context, System.Type destinationType)
        {
            if (destinationType == typeof(FloatRange))
                return true;
            return base.CanConvertTo(context, destinationType);
        }

        public override object ConvertTo(ITypeDescriptorContext context,
                               System.Globalization.CultureInfo culture,
                               object value,
                               System.Type destinationType)
        {
            if (destinationType == typeof(System.String) &&
                 value is FloatRange)
            {
                var so = (FloatRange)value;
                return so.ToString();
            }
            return base.ConvertTo(context, culture, value, destinationType);
        }
    }

    [TypeConverter(typeof(RangeConvertor))]
    public class FloatRange
    {
        public float MinValue { get; set; }
        public float MaxValue { get; set; }

        public FloatRange(float Min, float Max)
        {
            MinValue = Min;
            MaxValue = Max;
        }

        public FloatRange()
        {
            MinValue = 0;
            MaxValue = 1;
        }

        public static float GetValue(FloatRange _range, Random _random)
        {
            return ((float)_random.NextDouble() * (_range.MaxValue - _range.MinValue)) + _range.MinValue;
        }

        public override string ToString()
        {
            return "[ " + MinValue.ToString() + ", " + MaxValue.ToString() + " ]";
        }
    }
}
